package com.tanhua.sso.controller;

//import com.tanhua.sso.service.UserService;
//import com.tanhua.sso.vo.ErrorResult;
//import com.tanhua.sso.vo.LoginResult;

import com.tanhua.common.pojo.User;
import com.tanhua.sso.service.UserService;
import com.tanhua.sso.vo.ErrorResult;
import com.tanhua.sso.vo.LoginResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/*
 * 处理手机号登陆的接口
 * */
@RestController
@RequestMapping("user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;
    @PostMapping("login")
    public ResponseEntity<ErrorResult> login(@RequestBody Map<String, String> param) {
        String phone = param.get("phone");
        log.info("手机号为:{}", phone);
        ErrorResult result = userService.login(phone);
        //服务器有问题的时候
        //result为null表示验证码发送成功
        if (result==null){
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.status(500).body(result);
    }



//原始
//    @Autowired
//    private UserService userService;
//
//    @PostMapping("login")
//    public ResponseEntity<ErrorResult> login(@RequestBody Map<String,String> param){
//        //获取手机号
//        String phone = param.get("phone");
//        ErrorResult result = userService.login(phone);

//
    @PostMapping("loginVerification")
    public ResponseEntity<Object> loginVerification(@RequestBody Map<String,String> param){
        //获取手机号和验证码
        //获取手机号
        String phone = param.get("phone");
        String code = param.get("verificationCode");
        LoginResult result=userService.userLogin(phone,code);
        if (result!=null){
            return ResponseEntity.ok(result);
        }else {
            ErrorResult errorResult = ErrorResult.builder().errCode("000001").errMessage("验证码错误xxxxxxxxxxxx").build();
            return ResponseEntity.status(500).body(errorResult);
        }

    }
    /**
     * 校验token，根据token查询用户数据
     *
     * @param token
     * @return
     */
    @GetMapping("{token}")
    public User queryUserByToken(@PathVariable("token") String token) {
        return this.userService.queryToken(token);
    }
}
